//
//  UIViewController+NavBarHidden.swift
//  elarc01
//
//  Created by elarc on 16/5/4.
//  Copyright © 2016年 elarc. All rights reserved.
//

import Foundation


extension UIViewController {
    enum ElarcHidenControlOptions {
        case ElarcHidenControlOptionsLeft
        case ElarcHidenControlOptionsTittle
        case ElarcHidenControlOptionsRight
    }
    
    private struct AssociatedKeys {
        static var NavDescriptiveName = "navBarBackgroundImage"
        static var ScrollViewKey = "scrollViewKey"
        static var ScrolOffsetYKey = "offsetY"
        static var hy_hidenControlOptionsKey = "hy_hidenControlOptions"
    }
    
    var navBarBackgroundImage:UIImage {
        set {
          objc_setAssociatedObject(self, &AssociatedKeys.NavDescriptiveName, navBarBackgroundImage, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        get {
            return (objc_getAssociatedObject(self,&AssociatedKeys.NavDescriptiveName) as? UIImage)!
        }
    }
    
    var keyScrollView:UIScrollView? {
        set {
            objc_setAssociatedObject(self, &AssociatedKeys.ScrollViewKey, keyScrollView, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        get {
            return objc_getAssociatedObject(self,&AssociatedKeys.ScrollViewKey) as? UIScrollView
        }
    }
    
    var scrolOffsetY:CGFloat? {
        set {
            objc_setAssociatedObject(self, &AssociatedKeys.ScrolOffsetYKey, scrolOffsetY, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        get {
            return objc_getAssociatedObject(self,&AssociatedKeys.ScrolOffsetYKey) as? CGFloat
        }
    }
    
    var elarcHidenControlOptions:ElarcHidenControlOptions?{
        set {
            objc_setAssociatedObject(self, &AssociatedKeys.hy_hidenControlOptionsKey, elarcHidenControlOptions?.hashValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        get {
            return objc_getAssociatedObject(self,&AssociatedKeys.hy_hidenControlOptionsKey) as? ElarcHidenControlOptions
        }
    }
    
    func setKeyScrollView(keyScrollView: UIScrollView , scrolOffsetY: CGFloat , options :ElarcHidenControlOptions) -> Void {
        self.keyScrollView = keyScrollView
        self.elarcHidenControlOptions = options
        self.scrolOffsetY = scrolOffsetY
        
        self.keyScrollView?.addObserver(self, forKeyPath: "contentOffset", options: NSKeyValueObservingOptions.New, context: nil)
    }
   
    public override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        
        var alpha:CGFloat  = 0;
        var offsetY:CGFloat  = 0;
        
        offsetY = self.scrolOffsetY!
        let point:CGPoint  = self.keyScrollView!.contentOffset;
        alpha =  point.y/offsetY
        alpha = (alpha <= 0) ? 0 : alpha
        alpha = (alpha >= 1) ? 1 : alpha
        //设置导航条上的标签是否跟着透明
        self.navigationItem.leftBarButtonItem!.customView!.alpha = self.elarcHidenControlOptions?.hashValue == 1 ? alpha:1;
        self.navigationItem.titleView!.alpha = self.elarcHidenControlOptions?.hashValue == 2 ? alpha:1;
        self.navigationItem.rightBarButtonItem!.customView!.alpha = self.elarcHidenControlOptions?.hashValue == 3 ? alpha:1;
        
        self.navigationController!.navigationBar.subviews[0].alpha = alpha
        
    }
}